﻿using Sunny.UI;
using System.Collections.Generic;
using System.Windows.Forms.VisualStyles;

namespace CoreShop.PrintService
{
    public enum PageSize
    {
        W58 = 0,
        W76 = 1,
        W80 = 2
    }

    public static class PageSizeHelper
    {
        public static int CharCount(this PageSize pageSize, int FontSize = 1)
        {
            if (pageSize == PageSize.W58)
            {
                if (FontSize == 1) return 32;
                if (FontSize == 2) return 16;
                if (FontSize == 3) return 10;
            }

            if (pageSize == PageSize.W76)
            {
                if (FontSize == 1) return 44;
                if (FontSize == 2) return 22;
                if (FontSize == 3) return 12;
            }

            if (pageSize == PageSize.W80)
            {
                if (FontSize == 1) return 47;//if (FontSize == 1) return 48;
                if (FontSize == 2) return 47;//if (FontSize == 2) return 24;
                if (FontSize == 3) return 24;
            }

            return 27;
        }

        public static string DisplayText(this PageSize pageSize)
        {
            if (pageSize == PageSize.W58) return "58CM";
            if (pageSize == PageSize.W76) return "76CM";
            if (pageSize == PageSize.W80) return "80CM";
            return "";
        }
    }

    public class MenuItem
    {
        public int Weight { get; set; }

        public string Text { get; set; }

        public eTextAlignMode TextAlign { get; set; }

        public string DataPropertyName { get; set; }

        public eTextAlignMode DataAlign { get; set; }

        public MenuItem Clone()
        {
            MenuItem item = new MenuItem();
            item.Weight = Weight;

            item.Text = Text;
            item.TextAlign = TextAlign;

            item.DataPropertyName = DataPropertyName;
            item.DataAlign = DataAlign;
            return item;
        }
    }

    public class MenuLine
    {
        public List<MenuItem> Items = new List<MenuItem>();

        public string ID { get; set; }

        public bool IsLine { get; set; }

        public bool IsBlank { get; set; }

        public VerticalAlignment VerAlign { get; set; } = VerticalAlignment.Top;

        public int FontSize { get; set; } = 1;

        public MenuLine()
        {
            ID = RandomEx.RandomNumber(10);
        }

        public int GetTotalWeight()
        {
            int result = 0;
            foreach (var item in Items)
            {
                result += item.Weight;
            }

            return result;
        }

        public override string ToString()
        {
            string ver = "T";
            if (VerAlign == VerticalAlignment.Center) ver = "M";
            if (VerAlign == VerticalAlignment.Bottom) ver = "B";
            if (IsLine) return "[" + ver + "/" + "0] ------分割线------";
            if (IsBlank) return "[" + ver + "/" + "0] ------空白行------";
            List<string> list = new List<string>();
            foreach (var item in Items) list.Add(item.Text + "/" + item.DataPropertyName);
            return "[" + ver + "/" + Items.Count + "] " + string.Join("; ", list.ToArray());
        }

        public MenuLine Clone()
        {
            MenuLine line = new MenuLine();
            foreach (var item in Items)
            {
                line.Items.Add(item.Clone());
            }

            line.ID = ID;
            line.FontSize = FontSize;
            line.VerAlign = VerAlign;
            line.IsLine = IsLine;
            line.IsBlank = IsBlank;
            return line;
        }
    }

    public class Menu
    {
        public PageSize PageSize { get; set; }
        public string Name { get; set; }
        public string FontName { get; set; }

        public List<MenuLine> Header = new List<MenuLine>();

        public List<MenuLine> Center = new List<MenuLine>();

        public List<MenuLine> Footer = new List<MenuLine>();

        public string BarCode { get; set; }

        public string Image { get; set; }

        public bool Multi { get; set; }

        public void AddLine(MenuLine line)
        {
            if (line.VerAlign is VerticalAlignment.Top) Header.Add(line);

            if (line.VerAlign is VerticalAlignment.Center)
            {
                Center.Clear();
                Center.Add(line);
            }

            if (line.VerAlign is VerticalAlignment.Bottom) Footer.Add(line);
        }

        public void UpdateLine(MenuLine line)
        {
            if (line.VerAlign is VerticalAlignment.Top)
            {
                for (int i = 0; i < Header.Count; i++)
                {
                    if (Header[i].ID == line.ID) Header[i] = line;
                }
            }

            if (line.VerAlign is VerticalAlignment.Center)
            {
                Center.Clear();
                Center.Add(line);
            }

            if (line.VerAlign is VerticalAlignment.Bottom)
            {
                for (int i = 0; i < Footer.Count; i++)
                {
                    if (Footer[i].ID == line.ID) Footer[i] = line;
                }
            }
        }

        public void DeleteLine(MenuLine line)
        {
            if (line.VerAlign is VerticalAlignment.Top)
            {
                int idx = -1;
                for (int i = 0; i < Header.Count; i++)
                {
                    if (Header[i].ID == line.ID) idx = i;
                }

                if (idx >= 0) Header.RemoveAt(idx);
            }

            if (line.VerAlign is VerticalAlignment.Center)
            {
                Center.Clear();
            }

            if (line.VerAlign is VerticalAlignment.Bottom)
            {
                int idx = -1;
                for (int i = 0; i < Footer.Count; i++)
                {
                    if (Footer[i].ID == line.ID) idx = i;
                }

                if (idx >= 0) Footer.RemoveAt(idx);
            }
        }
    }
}
